<template>
	<view class="container">
		<view v-if="roleId && roleId != '1001' ">
			<employeeIndex 
				:filterGirdData="filterGirdData" :tags="tags"></employeeIndex>
		</view>
		<view v-else>
			<userIndex :roleId="roleId" :roleName="roleName" :userInfo="userInfo" :hasLogin="hasLogin"
				:filterGirdData="filterGirdData" :tags="tags">
			</userIndex>
		</view>
		<view class="clearfix"></view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
	import roleAuth from '@/common/data/role_auth/roleauth.js';
	import indexGirdData from '@/common/data/index/indexGirdData.js';

	import userIndex from '@/pages/index/userIndex.vue';
	import employeeIndex from '@/pages/index/employeeIndex.vue'
	export default {
		data() {
			return {
				roleAuth: roleAuth,
				girdData: indexGirdData,
				backData: indexGirdData,
				roleId: '',
				userRole: [],
				filterGirdData: [],
				datetimesingle: '',
				tags: {
					unPay: 0,
					unOut: 0
				},
				searchParam: {
					userId: null,
					storeIds: [],
					payStatusList: [],
					statusList: [],
					startDate: null,
					endDate: null,
				},
			};
		},

		components: {
			userIndex,
			employeeIndex
		},

		computed: {
			...mapGetters({
				userInfo: 'user/info',
				hasLogin: 'user/hasLogin'
			}),
		},
		onShow() {
			if(this.userInfo.roleId != this.roleId){
				this.init();
			}
			this.setCurrentPage(this);
			// 需要从缓存中获取用户是否已经登录h
			if (!this.hasLogin) {
				uni.navigateTo({
					url: "/pages/login/login"
				})
			}
		},

		created() {

		},

		onLoad(option) {
			this.setCurrentPage(this);
			if (option) {
				this.setData({
					globalOption: this.getOption(option)
				});
			}
			this.init();
		},
		methods: {
			changed(roleId, roleName) {
				this.roleId = roleId;
				this.roleName = roleName;
				this.init();
			},

			async init() {
				if (this.userInfo && this.userInfo.roleId) {
					this.roleId = this.userInfo.roleId;
				}
				this.tags.unOut = 0;
				this.tags.unPay = 0;
				// 查询数量标识
				this.queryOrderTag();
			},

			async queryOrderTag() {
				if (this.roleId === '1001') {
					// 会员，只能查询自己名下的订单
					this.searchParam.userId = this.userInfo.id;
				} else if (this.userInfo.storeIds) {
					// 有权限的门店
					this.searchParam.storeIds = this.userInfo.storeIds;
				}
				this.queryOrderCount();
			},

			queryOrderCount() {
				this.searchParam.payStatusList = [];
				this.searchParam.statusList = [];
				//  0 待取货 1 部分取货
				this.searchParam.payStatusList.push(0);
				// 查询待付款tag
				this.orderCount(this.searchParam, "unPay");
				//查询待出库tag
				this.searchParam.payStatusList = [];
				this.searchParam.statusList.push(0);
				this.searchParam.statusList.push(1);
				this.orderCount(this.searchParam, "unOut");
			},

			orderCount(searchParam, tag) {
				this.$api.orderCount(searchParam).then(result => {
					if(result && Number.isInteger(result) ){
						this.tags[tag] = result;
					}
				}).catch(res => {
					// 失败进行的操作
					uni.showToast({
						title: "查询失败!",
						duration: 3000
					});
					return;
				})
			},

			/**
			 * @param {Object} link
			 * 跳转页面
			 */
			naviTo(link) {
				uni.navigateTo({
					url: link,
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
</style>